package com.geektime.hello;

import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.URLUtil;
import cn.hutool.http.HttpConnection;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

public class HutoolDemo {
    public static void main(String[] args) throws IOException {
        List<String> list = new ArrayList<>();
        list.add("a");
        list.add("b");
        list.add("c");
        String str = StrUtil.join(",", list.toArray());
        System.out.println("str = " + str);

        String url = "https://ahtvoss.oss-cn-heyuan.aliyuncs.com/ahtvoss/20200910/7ab8167e-bd48-6b2e-3c49-2dc7417d7a04.mp4";
        String length = HttpRequest.get(url).contentLength();
        System.out.println("length = " + length);

        int length2 = getFileLength(url);
        System.out.println("length2 = " + length2);

        isAvailable();
    }

    private static boolean isAvailable() {
        URL url = null;
        try {
            url = new URL("http://piczz.cms.cntv.cn/cntv/20201208/90/18059997662522498570.jpg");
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            int code = conn.getResponseCode();
            System.out.println("code = " + code);
            if (code == 200) {
                return true;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }

    private static int getFileLength(String url1) throws IOException {
        int length = 0;
        URL url;
        try {
            url = new URL(url1);
            HttpURLConnection urlcon = (HttpURLConnection) url.openConnection();
            //根据响应获取文件大小
            length = urlcon.getContentLength();
            urlcon.disconnect();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return length;
    }
}
